package xzy.Controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import xzy.dao.GradeMapper;
import xzy.pojo.Grade;
import xzy.pojo.PageBean;
import xzy.util.JsonUtil;
import xzy.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "GradeList_Servlet")
public class GradeList_Servlet extends HttpServlet {
    Grade grade=new Grade();
    private static ApplicationContext context=new ClassPathXmlApplicationContext("application.xml");
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String page=request.getParameter("page");
        String rows=request.getParameter("rows");
        String gradeName=request.getParameter("gradeName");
        if(gradeName==null){
            gradeName="";
        }
        grade.setGradeName(gradeName);
        PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
        GradeMapper gradeMapper= (GradeMapper) context.getBean("gradeMapper");
        try{
            JSONObject result=new JSONObject();
            JSONArray jsonArray= JsonUtil.formatRsToJsonArray(gradeMapper.getGradeAll1(pageBean,grade));
            int total=gradeMapper.gradeCount();
            result.put("rows",jsonArray);
            result.put("total",total);
            ResponseUtil.write(response,result);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);

    }
}
